package com.haliyoo.adhere.activity;

import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
import java.net.URL;

import com.haliyoo.adhere.R;
import com.haliyoo.adhere.bean.WeiboBean;
import com.haliyoo.adhere.controller.AdUserConstant;
import com.haliyoo.adhere.controller.OAuthConstant;
import com.haliyoo.adhere.handler.MyCharmHandler;
import com.haliyoo.adhere.util.BitmapGet;
import com.haliyoo.adhere.widget.PromptDialog;

import weibo4android.User;
import weibo4android.Weibo;
import weibo4android.WeiboException;
import weibo4android.http.AccessToken;
import weibo4android.http.RequestToken;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class OfficialWeiboActivity extends Activity implements OnClickListener {
	private static String TAG = "OfficialWeiboActivity";
    private static String strUserId = "adherebar";
    private static final int MSG_SET_CHARM = 0;
    private SetCharmHandler setCharmHandler = new SetCharmHandler();
    private User user = null;
    private int nMyCreative = 0;
    private int nMyCharm = 0;
    private int nMyContributions = 0;
    private int nMyColleagues = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
    	this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.officialweibo);

        LayoutInflater inflater = LayoutInflater.from(OfficialWeiboActivity.this);
        
		// / 设置标题栏
		RelativeLayout rlTitle = (RelativeLayout) findViewById(R.id.main_title);

		View vTitle = inflater.inflate(R.layout.title_write_refresh, null);
		rlTitle.removeAllViews();
		rlTitle.addView(vTitle);

		// / 设置内容区
		LinearLayout llContent = (LinearLayout) findViewById(R.id.llContent);
		View vContent = inflater.inflate(R.layout.user_charm, null);
		llContent.removeAllViews();
		llContent.addView(vContent);

		/**
		 * 写微博键处理
		 */
		ImageButton ibWrite = (ImageButton) findViewById(R.id.IMAGE_BUTTON_WRITE);
		ibWrite.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(OfficialWeiboActivity.this,
						CreativeDesignActivity.class);
				startActivity(intent);
			}
		});

		/**
		 * 刷新微博键处理
		 */
		ImageButton ibRefresh = (ImageButton) findViewById(R.id.IMAGE_BUTTON_REFRESH);
		ibRefresh.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setCharmByUserId(strUserId);
			}
		});

        TextView tvTitle = (TextView) findViewById(R.id.TEXT_VIEW_TITLE);
        tvTitle.setText(getString(R.string.STR_OFFICIAL_WEIBO));
        
        /**
         * 设置魅力
         */
        setCharmByUserId(strUserId);
	}

	/**
	 * 设置用户魅力
	 * @param strUserId 用户ID
	 */
	public void setCharmByUserId(String strUserId) {

		this.strUserId = strUserId;
		
		/**
		 * 显示进度条
		 */
		refreshShowProgressBar(true);


        new Thread() {
            @Override
            public void run() {
        		/// 获取数目
            	if (AdUserConstant.getInstance().count(AdUserConstant.getInstance().user, 
            			AdUserConstant.getInstance().sign, AdUserConstant.getInstance().user) == 1) {
            		nMyCreative = AdUserConstant.creativeCount;
            		nMyCharm = AdUserConstant.charmCount;
            		nMyContributions = AdUserConstant.contributionsCount;
            		nMyColleagues = AdUserConstant.colleaguesCount;
            	}
            	
        		try {
        			user = OAuthConstant.getInstance().getWeibo().showUser(OfficialWeiboActivity.strUserId);
        		} catch (WeiboException e) {
        			// TODO Auto-generated catch block
        			e.printStackTrace();
        		}
        		
            	setCharmHandler.sendEmptyMessage(MSG_SET_CHARM);
            }
        }.start();
	}

    void refreshShowProgressBar(boolean isShow) {
		ImageButton ibRefresh = (ImageButton) findViewById(R.id.IMAGE_BUTTON_REFRESH);
		ProgressBar pbRefresh = (ProgressBar) findViewById(R.id.PROGRESS_BAR_WAITING);
		if (isShow) {
			ibRefresh.setVisibility(View.GONE);
			pbRefresh.setVisibility(View.VISIBLE);
		} else {
			ibRefresh.setVisibility(View.VISIBLE);
			pbRefresh.setVisibility(View.GONE);
		}
    }
    
	class SetCharmHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			if (msg.what == MSG_SET_CHARM) {
				setCharm();
				
				/**
				 * 隐藏进度条
				 */
				refreshShowProgressBar(false);
			}
			super.handleMessage(msg);
		}
	}
	
	/**
	 * 设置用户魅力
	 */
	public void setCharm() {

		if (user == null) {
			return;
		}

		/**
		 * 填写用户资料
		 */

		/// 头像
		ImageView ivProfile = (ImageView) findViewById(R.id.ivPortrait);
		// / 老版本SDK不能获取大图片
		Bitmap bitmap = BitmapGet.getHttpBitmap(user.getProfileImageURL());
		ivProfile.setImageBitmap(bitmap);// 设置Bitmap

		Log.i(TAG, "image uri:" + user.getAvatarLarge());

		// / 用户名
		TextView tvUser = (TextView) findViewById(R.id.tvNick);
		tvUser.setText(user.getName());

		// / 位置
		TextView tvLocation = (TextView) findViewById(R.id.tvAddress1_content);
		tvLocation.setText(user.getLocation());

		// / 签名
		TextView tvSignature = (TextView) findViewById(R.id.tvAccount_introduce_content);
		tvSignature.setText(user.getDescription());

		// / 性别
		ImageView ivGender = (ImageView) findViewById(R.id.ivGental);
		if (user.getGender().equals("m")) {
			ivGender.setImageResource(R.drawable.male);
		} else {
			ivGender.setImageResource(R.drawable.female);
		}

		/**
		 * 填充数据
		 */
		TextView tvMyCreative = (TextView) findViewById(R.id.tvAttention_count);
		TextView tvMyCharm = (TextView) findViewById(R.id.tvWeibo_count);
		TextView tvMyContributions = (TextView) findViewById(R.id.tvFans_count);
		TextView tvMyColleagues = (TextView) findViewById(R.id.tvTopic_count);

		tvMyCreative.setText(nMyCreative + "");
		tvMyCharm.setText(nMyCharm + "");
		tvMyContributions.setText(nMyContributions + "");
		tvMyColleagues.setText(nMyColleagues + "");

		LinearLayout llMyCreative = (LinearLayout) findViewById(R.id.llAttention);
		LinearLayout llMyCharm = (LinearLayout) findViewById(R.id.rlWeibo);
		LinearLayout llMyContributions = (LinearLayout) findViewById(R.id.llFans);
		LinearLayout llMyColleagues = (LinearLayout) findViewById(R.id.llTopic);
		llMyCreative.setOnClickListener(this);
		llMyCharm.setOnClickListener(this);
		llMyContributions.setOnClickListener(this);
		llMyColleagues.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		int id = v.getId(); //点击按钮的id
		Intent intent = new Intent();
		
		switch (id){
		/// 我的创意
		case R.id.llAttention:
			intent.putExtra("from", "MyCharmActivity");
			intent.putExtra("item", "my creative");
			intent.putExtra("title", getString(R.string.STR_MY_CREATIVE));
			intent.setClass(OfficialWeiboActivity.this, CreativeHallActivity.class);
			startActivity(intent);
			break;
			
		/// 我的魅力
		case R.id.rlWeibo:
			intent.putExtra("from", "MyCharmActivity");
			intent.putExtra("item", "my charm");
			intent.putExtra("title", getString(R.string.STR_MY_CHARM));
			intent.setClass(OfficialWeiboActivity.this, CreativeHallActivity.class);
			startActivity(intent);
			break;
			
		/// 我的贡献
		case R.id.llFans:
			intent.putExtra("from", "MyCharmActivity");
			intent.putExtra("item", "my contributions");
			intent.putExtra("title", getString(R.string.STR_MY_CONTRIBUTIONS));
			intent.setClass(OfficialWeiboActivity.this, CreativeHallActivity.class);
			startActivity(intent);
			break;
			
		/// 我的童鞋
		case R.id.llTopic:
			intent.putExtra("from", "MyCharmActivity");
			intent.putExtra("item", "my colleagues");
			intent.putExtra("title", getString(R.string.STR_MY_COLLEAGUES));
			intent.setClass(OfficialWeiboActivity.this, CreativeHallActivity.class);
			startActivity(intent);
			break;
			
		default:
			Log.e(TAG, "error id touched.");
			break;
		}
	}

}
